//
//  String+Category.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/4/20.
//  Copyright © 2018年 common. All rights reserved.
//

import Foundation
import UIKit
extension NSString {
    func isHaveChinese() -> Bool {
        for i in 0 ..< self.length {
            let a = self.character(at: i)
            if a > 0x4e00 && a < 0x9fff {
                return true
            }
        }
       return false
    }
    
    
    func sizeWithConstrainedSize(size:CGSize, font:UIFont, lineSpacing:CGFloat) -> CGSize{
        var expectedSize = CGSize.zero
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineBreakMode = NSLineBreakMode.byWordWrapping
        paragraphStyle.lineSpacing = lineSpacing
        let attributes = [
            NSAttributedStringKey(rawValue: NSAttributedStringKey.font.rawValue):  font, NSAttributedStringKey.paragraphStyle : paragraphStyle
            ] as [NSAttributedStringKey : Any]
        
        expectedSize = self.boundingRect(with: size, options: NSStringDrawingOptions(rawValue: NSStringDrawingOptions.RawValue(UInt8(NSStringDrawingOptions.usesLineFragmentOrigin.rawValue) |
            UInt8(NSStringDrawingOptions.usesFontLeading.rawValue))), attributes: attributes , context: nil).size
        return CGSize(width: ceil(expectedSize.width), height: ceil(expectedSize.height))
    }
}
